﻿using System;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

using Jungle.Code.DAL;
using Jungle.Code.Utils;
using Jungle.Prodamo.MostNeedFunctions;

namespace Jungle.Prodamo.Core
{
    public class UserManager
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionUserBase"].ConnectionString;

        #region Fileds
        private int userId;
        private string nickName;
        private string fname;
        private string iname;
        private string oname;
        private string email;
        private bool sex;
        private string organization;
        private string edrpou;
        private string country;
        private string zip;
        private string city;
        private string address;
        private string phone;
        private string faks;
        private DateTime birthDay;
        private string password;
        private bool usertType;
        private string fastKey;
        private string workTime;
        private string web;
        private string deliver;
        private string credit;
        private string logo;
        private string map;
        private bool vip = false;
        #endregion

        public List<UserManager> UserList;

        #region Properties
        public int UserId { get { return userId; } set { userId = value; } }
        public string NickName { get { return nickName; } set { nickName = value; } }
        public string Fname { get { return fname; } set { fname = value; } }
        public string Iname { get { return iname; } set { iname = value; } }
        public string Oname { get { return oname; } set { oname = value; } }
        public string Email { get { return email; } set { email = value; } }
        public bool Sex { get { return sex; } set { sex = value; } }
        public string Organization { get { return organization; } set { organization = value; } }
        public string Edrpou { get { return edrpou; } set { edrpou = value; } }
        public string Country { get { return country; } set { country = value; } }
        public string Zip { get { return zip; } set { zip = value; } }
        public string City { get { return city; } set { city = value; } }
        public string Address { get { return address; } set { address = value; } }
        public string Phone { get { return phone; } set { phone = value; } }
        public string Faks { get { return faks; } set { faks = value; } }
        public DateTime BirthDay { get { return birthDay; } set { birthDay = value; } }
        public string Password { get { return password; } set { password = value; } }
        public bool UsertType { get { return usertType; } set { usertType = value; } }
        public string FastKey { get { return fastKey; } set { fastKey = value; } }
        public string WorkTime { get { return workTime; } set { workTime = value; } }
        public string Web { get { return web; } set { web = value; } }
        public string Deliver { get { return deliver; } set { deliver = value; } }
        public string Credit { get { return credit; } set { credit = value; } }
        public string Logo { get { return logo; } set { logo = value; } }
        public string Map { get { return map; } set { map = value; } }
        public bool Vip { get { return vip; } set { vip = value; } }
        #endregion

        public void Add()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_AddUser", connect);
                SqlDataReader reader = null;
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@NickName", SqlDbType.NChar, 50).Value = nickName;
                command.Parameters.Add("@Fname", SqlDbType.NChar, 50).Value = fname;
                command.Parameters.Add("@Iname", SqlDbType.NChar, 50).Value = iname;
                command.Parameters.Add("@Oname", SqlDbType.NChar, 50).Value = oname;
                command.Parameters.Add("@Email", SqlDbType.NChar, 50).Value = email;
                command.Parameters.Add("@Sex", SqlDbType.Bit).Value = sex;
                command.Parameters.Add("@Organization", SqlDbType.NChar, 100).Value = organization;
                command.Parameters.Add("@Edrpou", SqlDbType.NChar, 50).Value = edrpou;
                command.Parameters.Add("@Country", SqlDbType.NChar, 50).Value = country;
                command.Parameters.Add("@Zip", SqlDbType.NChar, 10).Value = zip;
                command.Parameters.Add("@City", SqlDbType.NChar, 50).Value = city;
                command.Parameters.Add("@Address", SqlDbType.NChar, 50).Value = address;
                command.Parameters.Add("@Phone", SqlDbType.NChar, 50).Value = phone;
                command.Parameters.Add("@Faks", SqlDbType.NChar, 50).Value = faks;
                command.Parameters.Add("@BirthDay", SqlDbType.DateTime).Value = birthDay;
                command.Parameters.Add("@Password", SqlDbType.NChar, 32).Value = password;
                command.Parameters.Add("@UserType", SqlDbType.Bit).Value = usertType;
                command.Parameters.Add("@FastKey", SqlDbType.NChar, 6).Value = fastKey;
                command.Parameters.Add("@WorkTime", SqlDbType.NChar, 100).Value = workTime;
                command.Parameters.Add("@Web", SqlDbType.NChar, 100).Value = web;
                command.Parameters.Add("@Deliver", SqlDbType.Text).Value = deliver;
                command.Parameters.Add("@Credit", SqlDbType.Text).Value = credit;
                command.Parameters.Add("@Logo", SqlDbType.Text).Value = logo;
                command.Parameters.Add("@Map", SqlDbType.Text).Value = map;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    userId = Convert.ToInt32(reader.GetValue(0));
                }
                command.Connection.Close();
            }
        }
        public void Update()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_Update", connect);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
                command.Parameters.Add("@Fname", SqlDbType.NChar, 50).Value = fname;
                command.Parameters.Add("@Iname", SqlDbType.NChar, 50).Value = iname;
                command.Parameters.Add("@Oname", SqlDbType.NChar, 50).Value = oname;
                command.Parameters.Add("@Email", SqlDbType.NChar, 50).Value = email;
                command.Parameters.Add("@Sex", SqlDbType.Bit).Value = sex;
                command.Parameters.Add("@Organization", SqlDbType.NChar, 100).Value = organization;
                command.Parameters.Add("@Edrpou", SqlDbType.NChar, 50).Value = edrpou;
                command.Parameters.Add("@Country", SqlDbType.NChar, 50).Value = country;
                command.Parameters.Add("@Zip", SqlDbType.NChar, 10).Value = zip;
                command.Parameters.Add("@City", SqlDbType.NChar, 50).Value = city;
                command.Parameters.Add("@Address", SqlDbType.NChar, 50).Value = address;
                command.Parameters.Add("@Phone", SqlDbType.NChar, 50).Value = phone;
                command.Parameters.Add("@Faks", SqlDbType.NChar, 50).Value = faks;
                command.Parameters.Add("@BirthDay", SqlDbType.DateTime).Value = birthDay;
                command.Parameters.Add("@Password", SqlDbType.NChar, 32).Value = password;
                command.Parameters.Add("@WorkTime", SqlDbType.NChar, 100).Value = workTime;
                command.Parameters.Add("@Web", SqlDbType.NChar, 100).Value = web;
                command.Parameters.Add("@Deliver", SqlDbType.Text).Value = deliver;
                command.Parameters.Add("@Credit", SqlDbType.Text).Value = credit;
                command.Parameters.Add("@Logo", SqlDbType.Text).Value = logo;
                command.Parameters.Add("@Map", SqlDbType.Text).Value = map;
                command.Parameters.Add("@Vip", SqlDbType.Bit).Value = vip;

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public bool Login()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_GetByLoginPass", connect);
                SqlDataReader reader = null;
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@Nickname", SqlDbType.NChar, 50).Value = nickName;
                command.Parameters.Add("@password", SqlDbType.NChar, 32).Value = password;

                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    userId = Convert.ToInt32(reader.GetValue(0));
                    return true;
                }
                else return false;
                reader.Close();
                command.Connection.Close();
            }
        }
        public void Delete()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("msUserd_DelById");
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@id_user", SqlDbType.Int).Value = userId;

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public void GetById()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_GetById", connect);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@userId", SqlDbType.Int).Value = userId;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    nickName = reader["NickName"].ToString().Trim();
                    fname = reader["Fname"].ToString().Trim();
                    iname = reader["Iname"].ToString().Trim();
                    oname = reader["Oname"].ToString().Trim();
                    email = reader["Email"].ToString().Trim();
                    sex = (bool)reader["Sex"];
                    organization = reader["Organization"].ToString().Trim();
                    edrpou = reader["Edrpou"].ToString().Trim();
                    country = reader["Country"].ToString().Trim();
                    zip = reader["Zip"].ToString().Trim();
                    city = reader["City"].ToString().Trim();
                    address = reader["Address"].ToString().Trim();
                    phone = reader["Phone"].ToString().Trim();
                    faks = reader["Faks"].ToString().Trim();
                    birthDay = (DateTime)reader["BirthDay"];
                    usertType = (bool)reader["UserType"];
                    fastKey = reader["FastKey"].ToString().Trim();
                    password = reader["password"].ToString();
                    workTime = reader["WorkTime"].ToString().Trim();
                    web = reader["Web"].ToString().Trim();
                    deliver = reader["Deliver"].ToString().Trim();
                    credit = reader["Credit"].ToString().Trim();
                    logo = reader["Logo"].ToString().Trim();
                    map = reader["Map"].ToString().Trim();
                    vip = (bool)reader["Vip"];
                }
                reader.Close();
                command.Connection.Close();
            }
        }
        public void GetAll()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_GetAll", connect);
                SqlDataReader reader = null;

                UserList = new List<UserManager>();
                command.CommandType = CommandType.StoredProcedure;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    UserManager um = new UserManager();
                    um.userId = (int)reader["UserId"];
                    um.GetById();

                    UserList.Add(um);
                }
                reader.Close();
                command.Connection.Close();
            }
        }
        public void GetByType()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_GetByType", connect);
                SqlDataReader reader = null;

                UserList = new List<UserManager>();
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@UserType", SqlDbType.Bit).Value = usertType;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    UserManager um = new UserManager();
                    um.userId = (int)reader["UserId"];
                    um.GetById();

                    UserList.Add(um);
                }
                reader.Close();
                command.Connection.Close();
            }        
        }
        public void SendEmail(string to, string subject, string body)
        {
            MailMessage message = new MailMessage("info@prodamo.serenity.com.ua", to, subject, body);
            SmtpClient client = new SmtpClient("mail.prodamo.serenity.com.ua", 25);
            client.Credentials = new NetworkCredential("info@prodamo.serenity.com.ua", "secretpass2");

            client.Send(message);        
        }
        public void GetByFastKey()
        { 
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_GetByFastKey", connect);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@FastKey", SqlDbType.NChar, 6).Value = fastKey;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    userId = (int)reader["UserId"];
                    nickName = reader["NickName"].ToString().Trim();
                    fname = reader["Fname"].ToString().Trim();
                    iname = reader["Iname"].ToString().Trim();
                    oname = reader["Oname"].ToString().Trim();
                    email = reader["Email"].ToString().Trim();
                    sex = (bool)reader["Sex"];
                    organization = reader["Organization"].ToString().Trim();
                    edrpou = reader["Edrpou"].ToString().Trim();
                    country = reader["Country"].ToString().Trim();
                    zip = reader["Zip"].ToString().Trim();
                    city = reader["City"].ToString().Trim();
                    address = reader["Address"].ToString().Trim();
                    phone = reader["Phone"].ToString().Trim();
                    faks = reader["Faks"].ToString().Trim();
                    birthDay = (DateTime)reader["BirthDay"];
                    usertType = (bool)reader["UserType"];
                    fastKey = reader["FastKey"].ToString().Trim();
                    password = reader["password"].ToString();
                    workTime = reader["WorkTime"].ToString().Trim();
                    web = reader["Web"].ToString().Trim();
                    deliver = reader["Deliver"].ToString().Trim();
                    credit = reader["Credit"].ToString().Trim();
                    logo = reader["Logo"].ToString().Trim();
                    map = reader["Map"].ToString().Trim();
                    vip = (bool)reader["Vip"];
                }
                reader.Close();
                command.Connection.Close();
        }
        }
        public string UserLink()
        {
            string linkUserInfo = "<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/userinfo.aspx?uId=" + userId.ToString() + "\"> " + nickName + "</a>";
            return linkUserInfo;
        }
        public string CompanyLink()
        {
            string linkUserInfo = "<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/userinfo.aspx?uId=" + userId.ToString() + "\"> " + organization + "</a>";
            return linkUserInfo;
        }
        public bool CheckNickName()
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_CheckLogin", connect);
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter param_login = command.Parameters.Add("@NickName", SqlDbType.NChar, 50);
                SqlDataReader reader = null;
                param_login.Value = nickName;

                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read()) return false;
                else return true;
                reader.Close();
                command.Connection.Close();
            }
        }
    }
}